<template>
    <div class="legend-custom space-full"></div>
</template>
<script>
import * as echarts from 'echarts'
export default {
  data: () => ({
    pieData: [
      { value: 1048, name: '搜索引擎' },
      { value: 735, name: '直接访问' },
      { value: 580, name: '邮件营销' },
      { value: 484, name: '联盟广告' },
      { value: 300, name: '视频广告' }
    ]
  }),
  mounted () {
    this.makeChart()
  },
  methods: {
    makeChart () {
      const data = this.pieData
      const option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: 'center',
          right: '20%',
          orient: 'vertical',
          itemGap: 20,
          data: [],
          formatter: (name) => {
            return '{name|' + name + '}{value|' + data.find(item => {
              return item.name === name
            }).value + '}'
          },
          textStyle: {
            rich: {
              name: {
                color: '#000',
                fontSize: 16,
                fontWeight: 'bold',
                padding: [0, 0, 0, 20],
                width: 80
              },
              value: {
                color: '#7D3AF7',
                fontWeight: 'bold',
                fontSize: 16
              }
            }
          }
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['50%', '70%'],
            center: ['30%', '50%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data
          }
        ]
      }
      data.forEach(item => {
        option.legend.data.push({
          name: item.name,
          icon: 'image://' + require('../../../asset/svg/fireworks.svg')
        })
      })
      const myChart = echarts.init(document.querySelector('.legend-custom'))
      myChart.setOption(option)
    }
  }
}
</script>
<style lang="less" scoped>

</style>
